package com.breeze.blockchain.controller.app;

import cn.hutool.core.util.StrUtil;
import com.breeze.components.core.util.DateUtil;
import com.breeze.components.web.core.ResponseDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import java.util.Optional;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件记录表 控制层。
 *
 * @author breeze
 * @since 2024-05-13 10:08:23
 */
@RestController
@Tag(name = "文件接口")
@RequestMapping("api/app/file")
public class AppFileController {

  @Resource private FileStorageService fileStorageService;

  @Operation(summary = "附件上传,返回图片url")
  @PostMapping("upload")
  public ResponseDTO<String> upload(MultipartFile file) {
    FileInfo fileInfo = fileStorageService.of(file).setPath(DateUtil.dateFilePath("app")).upload();
    return ResponseDTO.ok(
        Optional.ofNullable(fileInfo).map(FileInfo::getUrl).orElse(StrUtil.EMPTY));
  }
}
